<?php
    $views = Arr::getValue($params, 'views', array());
    $cities = Arr::getValue($params, 'cities', array());
    $types = Arr::getValue($params, 'types', array());
    $conceptions = Arr::getValue($params, 'conceptions', array());
    $tables = Arr::getValue($params, 'tables', array());

    //$cntId = json_encode(Arr::getValue($params, 'byId', array()));
    $tableHtml = '';

    foreach ($views as $item) {
        $item['parent_name'] = $item['parent_id']? Arr::getValue(Arr::getValue($views, $item['parent_id'],array()), 'obj_name', '?') : 'Отдельное здание';
        $item['obj_type_text'] = array_key_exists($item['obj_type'], $types)? $types[$item['obj_type']]['name'].', '.$types[$item['obj_type']]['datatypename'] : $item['obj_type'];
        $item['city_name'] = $item['city_id']? Arr::getValue(Arr::getValue($cities, $item['city_id'], array()), 'city_name', '?') : 'Нет';
        $item['cnc_name'] = $item['conception']? Arr::getValue(Arr::getValue($conceptions, $item['conception'], array()), 'name', '?') : 'Нет';

        $item['id_obj'] = '&nbsp;';
        $item['tables'] = join(', ', Arr::getValue($tables, $item['id'], array()));

        //$gender = $cnt['gender']? 'M' : 'Ж';
        //$ava    = $cnt['photo']? $cnt['photo'] : APP_HTML_AVA_PATH.'0.png';
        //if ($cnt['rating']>0) $cnt['rating'] = '+'.$cnt['rating'];
$tableHtml .= <<<__HTML__
        <tr id="vw_item_{$item['id']}">
            <td><button type="button" class="btn btn-info button-edit-view" id="btnEdit_{$item['id']}"><i class="icon-edit icon-white"></i></button></td>
            <td>{$item['id']}</td>
            <td data-field="obj_name">{$item['obj_name']}</td>
            <td data-field="obj_type_text">{$item['obj_type_text']}</td>
            <td data-field="parent_name">{$item['parent_name']}</td>
            <td data-field="tables">{$item['tables']}</td>
            <td data-field="city_name">{$item['city_name']}</td>
            <td data-field="cnc_name">{$item['cnc_name']}</td>
        </tr>
__HTML__;
    }
?>

<?php
$e = Arr::getValue($params, 'error', false);

if ($e) {?>
<div class="alert alert-danger"> <?php echo $e; ?> </div>
<?php } ?>

<table class="table table-striped table-bordered table-condensed" id="vwListTable">
    <thead>
        <tr style="background: #e0e0e0;">
            <th>&nbsp;</th>
            <th>id</th>
            <th>Название</th>
            <th>Тип</th>
            <th>Помещение</th>
            <th>Таблицы</th>
            <th>Город</th>
            <th>Концепция</th>
        </tr>
    </thead>
    <tbody>
        <?php echo $tableHtml;?>
    </tbody>
</table>


<script>



function openEditVwForm(elm) {
    elm = elm || {id:0};
    var id = elm.id.replace('btnEdit_', '');

    openVwDialog(id, function(cdata) {
        cdata = cdata || {};
        updateVwListString(cdata);
    });


}



function updateVwListString(cdata) {
    cdata = cdata || false;

    if (!cdata.hasOwnProperty('vw_id')) return;
    var id = cdata.vw_id;

    $('#vw_item_'+id+' td').each(function(){
        var td = $(this);
        var f = td.attr('data-field');
        if (f && cdata.hasOwnProperty(f)) td.html(cdata[f]);
    });

}







$(function() {
    Elm.vwListTable = $('#vwListTable');

    $('#vwListTable button.button-edit-view').click(function() {
        openEditVwForm(this);
        return false;
    });
});


</script>
